using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace cs2335.lab5
{
    public class RadioGroup : IComponent
    {
        #region Variables
        protected Rectangle container = new Rectangle(0, 0, 100, 25);
        protected StringFormat labelFormat = new StringFormat(StringFormatFlags.NoClip);
        protected SolidBrush backColor = new SolidBrush(Color.FloralWhite);
        protected Font font = new Font("SansSerif", 10);
        protected CCTForm owner;
        protected bool hasFocus = false;
        protected bool hidden = false;
        protected string name;
        protected string text;

        protected List<RadioButton> buttons;
        protected int buttonWidth;
        protected int selectedIndex;
        #endregion

        #region Properties
        /// <summary>
        /// Tells whether or not a component is hidden. If it is hidden,
        /// it should not be receiving actions.
        /// </summary>
        public bool Hidden
        {
            get { return hidden; }
        }

        public Size Size
        {
            get { return container.Size; }
            set
            {
                container.Size = value;
                Invalidate();
            }
        }

        public Point Location
        {
            get { return container.Location; }
            set
            {
                container.Location = value;
                Invalidate();
            }
        }

        public Rectangle Bounds
        {
            get { return container; }
            set
            {
                container = value;
                Invalidate();
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public System.Drawing.Font Font
        {
            get { return font; }
            set { font = value; Invalidate(); }
        }

        public string Text
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }

        public CCTForm Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public Color BackColor
        {
            get { return backColor.Color; }
            set { backColor.Color = value; Invalidate(); }
        }
        #endregion

        #region Events
        public event EventHandler Click;
        public event EventHandler GotFocus;
        public event EventHandler KeyPress;
        public event EventHandler KeyDown;
        public event EventHandler LostFocus;
        public event EventHandler Paint;
        #endregion

        #region IComponent Members
        public void Invalidate()
        {
            if (owner != null)
            {
                owner.Invalidate();
            }
        }

        public void Hide()
        {
            hidden = true;
            Invalidate();
        }

        public void Show()
        {
            hidden = false;
            Invalidate();
        }

        public void OnPaint(PaintEventArgs e)
        {
            if (!hidden)
            {
                Graphics g = e.Graphics;
                SetContainerSize();
                g.DrawRectangle(Pens.DarkGoldenrod, container);
                int margin = 2;
                int y = container.Y;

                foreach (RadioButton r in buttons)
                {
                    r.Location = new Point(container.X + margin, y + margin);
                    r.OnPaint(e);
                    y += r.ButtonWidth + margin * 4;
                }

                if (hasFocus)
                {
                    //DrawFocusBorder(g);
                }

                if (Paint != null)
                {
                    Paint(this, e);
                }
            }

        }

        public void OnGotFocus(EventArgs e)
        {
            hasFocus = true;
            if (GotFocus != null)
            {
                GotFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnLostFocus(EventArgs e)
        {
            hasFocus = false;
            if (LostFocus != null)
            {
                LostFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        {
            foreach (RadioButton r in buttons)
            {
                if(r.Bounds.Contains(e.Location))
                {
                    if (selectedIndex != -1)
                    {
                        buttons[selectedIndex].Selected = false;
                    }
                    r.Selected = true;
                    selectedIndex = buttons.IndexOf(r);
                    Invalidate();
                    r.OnMouseClick(e);
                    break;
                }
            }
        }

        public void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            //Invokes a Click event if the user hits ENTER
            //  while this component has focus
            if (e.KeyChar == '\r')
            {
                if (Click != null)
                {
                    Click(this, e);
                }
            }

            if (KeyPress != null)
            {
                KeyPress(this, e);
            }
        }

        public void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (KeyDown != null)
            {
                KeyDown(this, e);
            }
        }
        #endregion

        public RadioGroup()
        {
            buttons = new List<RadioButton>();
            selectedIndex = -1;
            buttonWidth = 25;
        }

        public List<RadioButton> Buttons
        {
            get { return buttons; }
            set { buttons = value; }
        }

        public int ButtonWidth
        {
            get { return buttonWidth; }
            set { buttonWidth = value + (int)Pens.White.Width; }
        }

        public void SetContainerSize()
        {
            container.Height = buttons.Count * buttonWidth + buttons.Count * (buttons.Count + 1);
        }

        public void Add(RadioButton b)
        {
            /// dprather - I made it so that the button is added
            /// without creating a new button. This was needed because
            /// creating a new button destoys any events associated with it
            //RadioButton r = new RadioButton();
            //r.Text = b.Text;
            //r.Click += b.Click;
            //buttons.Add(r);

            buttons.Add(b);
            SetContainerSize();
        }
    }
}
